﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Collections;

namespace CitrixPVSWrapper
{

    [System.Management.Automation.Cmdlet("Run", "PvsImportOemLicenses", SupportsShouldProcess = true)]
    public class Run_PvsImportOemLicenses : System.Management.Automation.PSCmdlet
    {
        [Parameter(Mandatory = true)]
        public String Filename;

        protected override void ProcessRecord()
        {
            // Verbose and Debug Settings
            Boolean Verbose = this.MyInvocation.BoundParameters.ContainsKey("Verbose");
            WriteVerbose("Verbose Flag: " + Verbose);
            Boolean Debug = this.MyInvocation.BoundParameters.ContainsKey("Debug");
            WriteDebug("Debug Flag: " + Debug);

            // Collections to hold the queries and the results
            string TargetObject = "ImportOemLicenses";

            //Build the query object
            QueryDefinition RunQuery = new QueryDefinition(QueryActionType.Run, TargetObject);

            RunQuery.Parameters.Add("filename=\"" + this.Filename + "\"");

            string ShouldProcessMessage = String.Format("Import OEM licenses from file {0}", this.Filename);
           
            // Perform the actual Run and return any errors
            if (ShouldProcess(ShouldProcessMessage))
            {
                MCliReturnObject Return = PvsHelpers.ExecuteMcliAction(RunQuery, Verbose, Debug);
                if (Return.ReturnState == ReturnState.Error) { foreach(ErrorRecord Error in Return.Errors) { WriteError(Error); } }
            }
        }
    }
}
